/*
 * Copyright 2012 Gayan Perera
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package gap.maven.script.components;

import gap.maven.script.helpers.LinkerFactory;

import java.util.HashMap;
import java.util.List;

import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.PluginManager;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProjectBuilder;

public class ComponentContext {
    public final static String COMPONENT_KEY_ARTIFACT_FACTORY = "ARTIFACT_FACTORY";
    public final static String COMPONENT_KEY_ARTIFACT_RESOLVER = "ARTIFACT_RESOLVER";
    public final static String COMPONENT_KEY_PROJECT_BUILDER = "PROJECT_BUILDER";
    public final static String COMPONENT_KEY_LOCAL_REPOSITORY = "LOCAL_REPOSITORY";
    public final static String COMPONENT_KEY_REMOTE_REPOSITORY_LIST = "REMOTE_REPOSITORY_LIST";
    public final static String COMPONENT_KEY_MAVEN_SESSION = "MAVEN_SESSION";
    public final static String COMPONENT_KEY_PLUGIN_MANAGER = "PLUGIN_MANAGER";
    public final static String COMPONENT_KEY_LOG = "LOG";
    public final static String COMPONENT_KEY_LINKER = "LINKER";
    public final static String COMPONENT_KEY_FILE_EXTRACTOR = "FILE_EXTRACTOR";
    
    
    private final HashMap<String, Object> components;
    
    @SuppressWarnings("rawtypes")
    public ComponentContext(Log log,
            ArtifactFactory artifactFactory, ArtifactResolver resolver, MavenProjectBuilder projectBuilder,
            ArtifactRepository localRepository, List remoteRepositories, MavenSession session, PluginManager pluginManager) {
        components = new HashMap<String, Object>();
        components.put(COMPONENT_KEY_ARTIFACT_FACTORY, artifactFactory);
        components.put(COMPONENT_KEY_ARTIFACT_RESOLVER, resolver);
        components.put(COMPONENT_KEY_LOCAL_REPOSITORY, localRepository);
        components.put(COMPONENT_KEY_PROJECT_BUILDER, projectBuilder);
        components.put(COMPONENT_KEY_REMOTE_REPOSITORY_LIST, remoteRepositories);
        components.put(COMPONENT_KEY_LOG, log);
        components.put(COMPONENT_KEY_LINKER, LinkerFactory.getInstance().createLinker(log));
        components.put(COMPONENT_KEY_MAVEN_SESSION, session);
        components.put(COMPONENT_KEY_PLUGIN_MANAGER, pluginManager);

        // TODO: this needs to be able to plugin by scripts with different impls.
        components.put(COMPONENT_KEY_FILE_EXTRACTOR, new DefaultFileExtractor(log));
    }

    @SuppressWarnings("unchecked")
    public <T> T lookup(String componentKey) {
        return (T) components.get(componentKey);
    }
}
